package com.draemon.haixiawenti.mvc;

import com.draemon.haixiawenti.exception.NoResponseException;
import com.draemon.haixiawenti.repository.*;
import com.draemon.haixiawenti.vo.*;
import com.yangzcp.userpojo.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhou on 2016/11/13.
 */
@RestController
public class AllInController {

    @Autowired
    private B_base_configRepository b_base_configRepository;

    @Autowired
    private B_saichengRepository b_saichengRepository;

    @Autowired
    private B_saiimgRepository b_saiimgRepository;

    @Autowired
    private B_saiimg_imgRepository b_saiimg_imgRepository;

    @Autowired
    private B_saizhongRepository b_saizhongRepository;

    @Autowired
    private B_saizu_ggRepository b_saizu_ggRepository;

    @Autowired
    private B_dasaiRepository b_dasaiRepository;

    @Autowired
    private B_zhanjiRepository b_zhanjiRepository;

    @Autowired
    private B_saizu_qiuduiRepository b_saizu_qiuduiRepository;

    @Autowired
    private B_qiuduiRepository b_qiuduiRepository;

    @Autowired
    private B_saizuRepository b_saizuRepository;

    @Autowired
    private B_qiuyuanRepository b_qiuyuanRepository;

    @Autowired
    private B_jinqiuRepository b_jinqiuRepository;

    @Autowired
    private B_fapaiRepository b_fapaiRepository;

    @Autowired
    private NewsRepository newsRepository;

    @Autowired
    private AppsRepository appsRepository;

    @Autowired
    private GonggaoRepository gonggaoRepository;

    @Autowired
    private B_wulongRepository b_wulongRepository;

    @Autowired
    private HqbnewsRepository hqbnewsRepository;

    private Integer DIAN_QIU_VALUE = 100010001;


    @RequestMapping(value = "/dasai/list", method = RequestMethod.GET)
    @ApiOperation("大赛列表")
    public List<B_dasai> dasai_list() {
        List<B_dasai> result = b_dasaiRepository.findAll();
        return result;
    }

    @ApiOperation("赛组列表")
    @RequestMapping(value = "/dasai/{id}/saizu/list", method = RequestMethod.GET)
    public List<B_saizu> dasai_saizu(@ApiParam("大赛ID") @PathVariable Integer id) {
        List<B_saizu> result = b_saizuRepository.findCond(id);
        return result;
    }

    @ApiOperation("赛种列表")
    @RequestMapping(value = "/saizu/{id}/saizhong/list", method = RequestMethod.GET)
    public List<B_saizhong> saizhong_list(@ApiParam("赛组ID") @PathVariable Integer id) {
        List<B_saizhong> result = b_saizhongRepository.findCond(id);
        return result;
    }
    @ApiOperation("积分排行榜-赛组")
    @RequestMapping(value = "/saizu/jifen/top/{id}", method = RequestMethod.GET)
    public SaizuJifenVO[] saizu_jifen_top(@ApiParam("赛组ID") @PathVariable Integer id) {
        List<SaizuJifenVO> result = new ArrayList<SaizuJifenVO>();
        List<B_saizhong> cond = b_saizhongRepository.findCond(id);
        for (B_saizhong b_saizhong : cond) {
            SaizuJifenVO item = new SaizuJifenVO();
            List<ZhanjiVO> zhanjiVOs = this.jifen_top(b_saizhong.getId());
            item.setB_saizhong(b_saizhong);
            item.setData(zhanjiVOs);
            result.add(item);
        }
        return result.toArray(new SaizuJifenVO[]{});
    }

    @ApiOperation("积分排行榜")
    @RequestMapping(value = "/jifen/top/{id}", method = RequestMethod.GET)
    public List<ZhanjiVO> jifen_top(@ApiParam("赛种ID") @PathVariable Integer id) {
        List<ZhanjiVO> result = new ArrayList<ZhanjiVO>();
        List<String> labelGroup = b_zhanjiRepository.findLabelGroup(id);
        for (String label : labelGroup) {
            ZhanjiVO zhanjiVO = new ZhanjiVO();
            List<Map<String, Object>> items = b_zhanjiRepository.findCond(id, label);
            zhanjiVO.setLabel(label);
            zhanjiVO.setJifenList(items);
            result.add(zhanjiVO);
        }
        return result;
    }

    @ApiOperation("积分排行榜-不分组")
    @RequestMapping(value = "/jifen/top/ungroup/{id}", method = RequestMethod.GET)
    public List<ZhanjiVO> jifen_top_ungroup(@ApiParam("赛种ID") @PathVariable Integer id) {
        List<ZhanjiVO> result = new ArrayList<ZhanjiVO>();
        ZhanjiVO zhanjiVO = new ZhanjiVO();
        List<Map<String, Object>> items = b_zhanjiRepository.findCond(id);
        zhanjiVO.setLabel("不分组");
        zhanjiVO.setJifenList(items);
        result.add(zhanjiVO);
        return result;
    }


    @ApiOperation("射手排行榜-赛组")
    @RequestMapping(value = "/saizu/sheshou/top/{id}", method = RequestMethod.GET)
    public SaizuSheshouVO[] saizu_sheshou_top(@ApiParam("赛组ID") @PathVariable Integer id) {
        List<SaizuSheshouVO> result = new ArrayList<SaizuSheshouVO>();
        List<B_saizhong> cond = b_saizhongRepository.findCond(id);
        for (B_saizhong b_saizhong : cond) {
            SaizuSheshouVO item = new SaizuSheshouVO();
            List<SheshouVO> sheshouVOs = this.sheshou_top(b_saizhong.getId());
            item.setB_saizhong(b_saizhong);
            item.setData(sheshouVOs);
            result.add(item);
        }
        return result.toArray(new SaizuSheshouVO[]{});
    }
    @ApiOperation("射手排行榜")
    @RequestMapping(value = "/sheshou/top/{id}", method = RequestMethod.GET)
    public List<SheshouVO> sheshou_top(@ApiParam("赛种ID") @PathVariable Integer id) {
        List<SheshouVO> result = new ArrayList<SheshouVO>();
        List<Map<String, Object>> jinQiuBan = b_jinqiuRepository.findJinQiuBan(id, DIAN_QIU_VALUE);
        List<Map<String, Object>> dianQiuban = b_jinqiuRepository.findDianQiuban(id, DIAN_QIU_VALUE);
        int i = 0;
        for (Map<String, Object> jin : jinQiuBan) {
            i++;
            SheshouVO sheshouVO = new SheshouVO();
            String qiuyuan = String.valueOf(jin.get("qiuyuan"));
            String qiudui = String.valueOf(jin.get("qiudui"));
            String qiuyuan_photo = String.valueOf(jin.get("qiuyuan_photo"));
            String qiudui_logo = String.valueOf(jin.get("qiudui_logo"));
            int jinnum = Integer.valueOf(String.valueOf(jin.get("num")));
            int diannum = 0;
            for (Map<String, Object> dianqiu : dianQiuban) {
                String qiuyuan1 = String.valueOf(dianqiu.get("qiuyuan"));
                String qiudui1 = String.valueOf(dianqiu.get("qiudui"));
                if(qiudui.endsWith(qiudui1) && qiuyuan.equals(qiuyuan1)){
                    diannum = Integer.valueOf(String.valueOf(dianqiu.get("num")));
                    break;
                }
            }
            sheshouVO.setPaiming(i);
            sheshouVO.setQiudui(qiudui);
            sheshouVO.setQiuyuan(qiuyuan);
            sheshouVO.setJinqiu(jinnum);
            sheshouVO.setDianqiu(diannum);
            sheshouVO.setQiuyuan_photo(qiuyuan_photo);
            sheshouVO.setQiudui_logo(qiudui_logo);
            result.add(sheshouVO);
        }
        return result;
    }

    @ApiOperation("赛程列表")
    @RequestMapping(value = "/saizu/{id}/saicheng/list", method = RequestMethod.GET)
    public List<B_saicheng> saicheng_list(@PathVariable Integer id) {
        List<B_saicheng> result = b_saichengRepository.findCond(id);
        return result;
    }

    @ApiOperation("赛程列表-赛种")
    @RequestMapping(value = "/saizhong/{id}/saicheng/list", method = RequestMethod.GET)
    public List<B_saicheng> saicheng_saizhong_list(@PathVariable Integer id) {
        List<B_saicheng> result = b_saichengRepository.findsaizhongCond(id);
        return result;
    }

    @RequestMapping(value = "/saicheng/{id}", method = RequestMethod.GET)
    @ApiOperation("赛程")
    public SaichengVO saicheng(@ApiParam("赛程") @PathVariable Integer id) {
        SaichengVO result = new SaichengVO();
        B_saicheng b_saicheng = b_saichengRepository.findOne(id);
        List<B_jinqiu> bysaicheng_a = b_jinqiuRepository.findBysaicheng(id, b_saicheng.getQiudui_a().getId());
        List<B_jinqiu> bysaicheng_b = b_jinqiuRepository.findBysaicheng(id, b_saicheng.getQiudui_b().getId());
        List<B_fapai> byfapai_a = b_fapaiRepository.findBysaicheng(id, b_saicheng.getQiudui_a().getId());
        List<B_fapai> byfapai_b = b_fapaiRepository.findBysaicheng(id, b_saicheng.getQiudui_b().getId());
        int a_wulong = b_wulongRepository.countWuLong(b_saicheng.getId(), b_saicheng.getQiudui_a().getId());
        int b_wulong = b_wulongRepository.countWuLong(b_saicheng.getId(), b_saicheng.getQiudui_b().getId());
        result.setB_saicheng(b_saicheng);
        result.setFapaiList_a(byfapai_a);
        result.setFapaiList_b(byfapai_b);
        result.setJinqiuList_a(bysaicheng_a);
        result.setJinqiuList_b(bysaicheng_b);
        result.setA_wulong(a_wulong);
        result.setB_wulong(b_wulong);
        return result;
    }

    @RequestMapping(value = "/saizu/{id}/saiig/list", method = RequestMethod.GET)
    @ApiOperation("图集列表")
    public List<SaiimgVO> saiimg_list(@ApiParam("赛组ID") @PathVariable Integer id) {
        List<SaiimgVO> result = new ArrayList<SaiimgVO>();
        List<B_saiimg> tuji = b_saiimgRepository.findCond(id);
        for (B_saiimg b_saiimg : tuji) {
            SaiimgVO saiimgVO = new SaiimgVO();
            List<B_saiimg_img> b_saiimg_imgs = b_saiimg_imgRepository.findCond(b_saiimg.getId());
            saiimgVO.setImgs(b_saiimg_imgs);
            saiimgVO.setSaiimg(b_saiimg);
            result.add(saiimgVO);
        }
        return result;
    }

    @RequestMapping(value = "/saizu/{id}/qiudui/list", method = RequestMethod.GET)
    @ApiOperation("球队列表")
    public List<B_qiudui> qiudui_list(@ApiParam("赛组ID") @PathVariable Integer id) {
        List<B_qiudui> result = new ArrayList<B_qiudui>();
        List<B_saizu_qiudui> items = b_saizu_qiuduiRepository.findCond(id);
        for (B_saizu_qiudui b_saizu_qiudui : items) {
            result.add(b_saizu_qiudui.getRef_qiudui());
        }
        return result;
    }

    @RequestMapping(value = "/qiudui/{id}", method = RequestMethod.GET)
    @ApiOperation("球队")
    public QiuduiVO qiudui(@ApiParam("球队ID") @PathVariable Integer id) {
        QiuduiVO result = new QiuduiVO();
        B_qiudui qiudui = b_qiuduiRepository.findOne(id);
        List<B_qiuyuan> qiuyuanList = b_qiuyuanRepository.findCond(id);
        result.setQiudui(qiudui);
        result.setQiuyuanList(qiuyuanList);
        return result;
    }

    @RequestMapping(value = "/saizu/{id}/guangao", method = RequestMethod.GET)
    @ApiOperation("赛组广告")
    public B_saizu_gg saizu_guangao(@ApiParam("赛组ID") @PathVariable Integer id) {
        B_saizu_gg result = b_saizu_ggRepository.findCond(id);
        if(result != null){
            return result;
        }
        throw new NoResponseException();
    }

    @RequestMapping(value = "/base_config", method = RequestMethod.GET)
    @ApiOperation("基本设置")
    public B_base_config base_config() {
        List<B_base_config> all = b_base_configRepository.findAll();
        if(!CollectionUtils.isEmpty(all)){
            return all.get(0);
        }else{
            throw new NoResponseException();
        }
    }

    @RequestMapping(value = "/news/list", method = RequestMethod.GET)
    @ApiOperation("新闻列表")
    public List<News> news_list() {
        List<News> all = newsRepository.findCond();
        return all;
    }

    @RequestMapping(value = "/news/{id}", method = RequestMethod.GET)
    @ApiOperation("获取新闻")
    public News news_get(@ApiParam("新闻ID") @PathVariable Integer id) {
        News all = newsRepository.findOne(id);
        if(all != null){
            return all;
        }else{
            throw new NoResponseException();
        }
    }

    @RequestMapping(value = "/hqbnews/list", method = RequestMethod.GET)
    @ApiOperation("海青杯新闻列表")
    public List<Hqbnews> haiqingbeinews_list() {
        List<Hqbnews> all = hqbnewsRepository.findCond();
        return all;
    }

    @RequestMapping(value = "/hqbnews/{id}", method = RequestMethod.GET)
    @ApiOperation("获取海青杯新闻")
    public Hqbnews haiqingbeinews_get(@ApiParam("新闻ID") @PathVariable Integer id) {
        Hqbnews all = hqbnewsRepository.findOne(id);
        if(all != null){
            return all;
        }else{
            throw new NoResponseException();
        }
    }

    @RequestMapping(value = "/apps/list", method = RequestMethod.GET)
    @ApiOperation("应用盒子列表")
    public List<Apps> apps_list() {
        List<Apps> list = appsRepository.findCond();
        return list;
    }

    @RequestMapping(value = "/gonggao/list", method = RequestMethod.GET)
    @ApiOperation("公告列表")
    public List<Gonggao> gonggao_list() {
        List<Gonggao> all = gonggaoRepository.findCond();
        return all;
    }

    @RequestMapping(value = "/gonggao/{id}", method = RequestMethod.GET)
    @ApiOperation("获取公告")
    public Gonggao gonggao_get(@ApiParam("公告ID") @PathVariable Integer id) {
        Gonggao all = gonggaoRepository.findOne(id);
        if(all != null){
            return all;
        }else{
            throw new NoResponseException();
        }
    }

}
